package LeetCode;

import java.util.*;

/**
 * @Author: fzykd
 * Date: 2024-04-13
 * Time: 22:40
 * User: LQH
 */


//最长连续序列
public class Longest {
    public static int longestConsecutive(int[] nums) {
        //1.数组放到集合当中去 去重  遍历数组 然后add到Set集合当中

        System.out.println("nums=" + Arrays.toString(nums));
        Set<Integer> set = new HashSet();
        for(int num : nums){
            set.add(num);
        }
        System.out.println("set=" + set);

        //创建计数变量
        int longCount = 0;

        //遍历Set集合 去判断连续的元素个数
        for(int n : set){
            if(!set.contains(n-1)){
                int currentNum = n;
                int currentStreak = 1;

                while(set.contains(currentNum + 1)){
                    currentNum += 1;
                    currentStreak += 1;
                }

                longCount = Math.max(longCount,currentStreak);

            }
        }
        return longCount;

    }

    public static void main(String[] args) {
        //逆天 恰好这几个数字 到set数组当中就是有序的
        int[] arr = {9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6};

        System.out.println(longestConsecutive(arr));


    }

}
